<?php

/**
 * ModifyShift Function
 *
 * Modify an existing shift
 *
 * @author Max Sibbald <maxsibbald@gmail.com>
 */
include_once 'UTerrorcode.php';
include_once 'UTcheckAuth.php';
include_once 'UTDBGlobal.php';

/**
 * @param $authUser string
 * @param $authUserPass string
 * @param $roleID string
 * @param $dateID string
 * @param $empID string
 * @param $fieldToModify string
 * @param $newValue
 * @return array
 */
function modifyShift($authUser, $authUserPass, $roleID, $dateID, $empID, $fieldToModify, $newValue) {
   global $dbaddress;
   global $dbuser;
   global $dbpassword;
   global $dbdatabasename;

   if (checkAuthentication($authUser, $authUserPass) != true) {
      return array(ErrorCode::authFailRead);
   }

   $conn = mysqli_connect($dbaddress, $dbuser, $dbpassword, $dbdatabasename);
   if (!$conn) {
      return array(ErrorCode::sysError);
   }

   switch ($fieldToModify) {
      case 'empID':
         $presql = "SELECT empID FROM paradigmshift_dev.Account WHERE accName = '$newValue' LIMIT 1";

         $result = mysqli_query($conn, $presql);
         if ($result) {
            $row = mysqli_fetch_row($result);
            $newValue = $row[0];
         } else {
            return array(ErrorCode::failUpdate + 700);
         }

         $sql = "UPDATE RoleDate SET empID =? WHERE roleID=? AND dateID=? AND empID=?";
         break;
      case 'dateID':
         //try to get the dateID
         $datestamp = $newValue;
         $presql = "SELECT dateID FROM paradigmshift_dev.Dates WHERE datestamp = '" . $datestamp . "' LIMIT 1";
         $result = mysqli_query($conn, $presql);
         if ($result->num_rows > 0) {
            $row = mysqli_fetch_row($result);
            $newValue = $row[0];
         } else {
            //date entry does not exist yet so create one
            //get the max date and increment
            $presql = "SELECT MAX(dateID) FROM paradigmshift_dev.Dates";
            $result = mysqli_query($conn, $presql);
            $row = mysqli_fetch_row($result);
            $newValue = $row[0] + 1;

            $presql = "INSERT INTO paradigmshift_dev.Dates (dateID, datestamp) VALUES($newValue, '" . $datestamp . "')";
            mysqli_query($conn, $presql);
         }

         $sql = "UPDATE RoleDate SET dateID =? WHERE roleID=? AND dateID=? AND empID=?";
         break;
      case 'roleID':
         $sql = "UPDATE RoleDate SET roleID =? WHERE roleID=? AND dateID=? AND empID=?";
         break;
      case 'shiftStatus':
         $sql = "UPDATE RoleDate SET shiftStatus =? WHERE roleID=? AND dateID=? AND empID=?";
         break;
      case 'startTime':
         $sql = "UPDATE RoleDate SET startTime =? WHERE roleID=? AND dateID=? AND empID=?";
         break;
      case 'endTime':
         $sql = "UPDATE RoleDate SET endTime =? WHERE roleID=? AND dateID=? AND empID=?";
         break;
      default:
         return array(ErrorCode::failUpdate);
   }

   $stmt = mysqli_stmt_init($conn);
   mysqli_stmt_prepare($stmt, $sql);
   mysqli_stmt_bind_param($stmt, "ssss", $newValue, $roleID, $dateID, $empID);
   mysqli_stmt_execute($stmt);

   if (mysqli_stmt_errno($stmt) == 0) {
      $code = array(ErrorCode::successUpdate);
   } else {
      $code = array(ErrorCode::failUpdate);
   }
   mysqli_stmt_close($stmt);
   return $code;
}

?>